package com.foreverwin.jzymes.production.inspect.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.NcData;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.common.util.CollectionUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.inspect.service.IInspectAlarmDisposeAdviceService;
import com.foreverwin.jzymes.qc.check.domain.QcCheck;
import com.foreverwin.jzymes.qc.check.domain.QcCheckFaiFail;
import com.foreverwin.jzymes.qc.check.service.IQcCheckAdiviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/inspect")
public class InspectAlarmDisposeController {

    @Autowired
    IInspectAlarmDisposeAdviceService inspectAlarmDisposeAdviceService;
    @Autowired
    INCDataAdviceService ncDataAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    INCCodeAdviceService ncCodeAdviceService;
    @Autowired
    IQcCheckAdiviceService qcCheckAdiviceService;

    @RequestMapping("/disposeInspectAlarm")
    public AjaxResult disposeInspectAlarm( @RequestBody Map<String,Object> jsonMap ){
        jsonMap.put( "site", CommonMethods.getSite() );
        try{
            jsonMap.put( "carrier", CarrierUtil.buildCarrier( (String)jsonMap.get( "site" ), (String)jsonMap.get( "carrier" ) ) );
            inspectAlarmDisposeAdviceService.disposeAlarmNC( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 查询本体不合格列表
     *
     * @param carrier
     * @return
     */
    @RequestMapping("/retrieveCarrierNC")
    public AjaxResult retrieveCarrierNC( @RequestParam String carrier ){
        try{
            String site = CommonMethods.getSite();
            Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
            carrier = CarrierUtil.buildCarrier( site, carrier );

            List<NcData> ncDataList = ncDataAdviceService.findOpenNcDataFromDate( site, carrier );
            List<Map<String,Object>> rtnNcDataList = new ArrayList<>();
            for( NcData ncData : ncDataList ){
                Map<String,Object> rtnNcDataMap = new HashMap<>();
                Map<String,Object> itemMap = itemAdviceService.existItem( ncData.getItemBo() );
                rtnNcDataMap.put( "itemBo", itemMap.get( "HANDLE" ) );
                rtnNcDataMap.put( "item", itemMap.get( "ITEM" ) );
                rtnNcDataMap.put( "revision", itemMap.get( "REVISION" ) );
                rtnNcDataMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
                Map<String,Object> ncCodeMap = ncCodeAdviceService.existNCCode( site, ncData.getNcCode() );
                rtnNcDataMap.put( "ncCode", ncCodeMap.get( "NC_CODE" ) );
                rtnNcDataMap.put( "ncDesc", ncCodeMap.get( "DESCRIPTION" ) );
                rtnNcDataMap.put( "operation", ncData.getOperation() );
                rtnNcDataMap.put( "disposition", ncData.getDisposition() );
                rtnNcDataList.add( rtnNcDataMap );
            }
            CollectionUtil.sort( rtnNcDataList, "description" );

            List<QcCheckFaiFail> qcCheckFaiFailList = new ArrayList<>();
            if( rtnNcDataList.size() > 0 ){
                Map<String,Object> faiData = qcCheckAdiviceService.findFaiDataByCarrier( site, carrier );
                if( faiData != null ){
                    QcCheck qcCheck = new QcCheck();
                    qcCheck.setSite( site );
                    qcCheckFaiFailList = qcCheckAdiviceService.resolveFaiData( qcCheck, faiData );
                }
            }

            Map<String,Object> returnMap = new HashMap<>();
            returnMap.put( "ncDetailList", rtnNcDataList );
            returnMap.put( "qcCheckFaiFailList", qcCheckFaiFailList );

            return AjaxResult.returnSuccess().put( "value", returnMap );
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

}
